package com.hshx.shiro;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * Created by tym
 */
public class MD5Pwd {

    public static final String ALGORITHM_NAME = "MD5";   // 基础散列算法
    public static final int HASH_ITERATIONS = 12;        // 自定义散列次数

    /**
     * MD5加密
     * @param username
     * @param password
     * @return
     */
    public static String MD5Pwd(String username, String password) {
        // salt盐值 username
        ByteSource salt = ByteSource.Util.bytes(username);
        String md5Pwd = new SimpleHash(ALGORITHM_NAME, password,
                salt, HASH_ITERATIONS).toHex();
//        md5Pwd = new SimpleHash(ALGORITHM_NAME,password).toHex();
        //System.out.println("加密："+md5Pwd);
        return md5Pwd;
    }

    public static void main(String[] args) {
        //加密的字符串
//        String pwd = "123456";
//
//        System.out.println("admin:" + MD5Pwd("admin", pwd));
//        System.out.println("accp:" + MD5Pwd("accp", pwd));
        //原始密码
        String source = "admin";
        //盐
        String salt = "admin";
        //散列次数
        int hashIterations = 12;
        //散列一次:1b2814e7f4cbb32fea953252e45fface
        //散列两次:174aec66f457f5169dbb1922393b6b4c
        //构造方法中:
        //第一个参数:明文，原始密码
        //第二个参数:盐，通过使用随机数
        //第三个参数:散列的次数，比如散列两次，相当于md5(md5(''))
        Md5Hash md5Hash = new Md5Hash(source, salt, hashIterations);

        String password_md5 = md5Hash.toString();
        System.out.println(password_md5);

        //第一个参数:散列算法
        SimpleHash simpleHash = new SimpleHash("md5", source, salt, hashIterations);
    }
}
